import os
import plistlib

dir_name ='Placements'

def getValue(plist_path,file_path):
    png_frames_info = {}
    png_file_path = []
    png_info = {}
    index = 0
    for sub_dir in os.listdir(plist_path):
        if sub_dir.endswith(".png"):
            png_full_path = os.path.join(plist_path, sub_dir)
            png_file_path.append(png_full_path)
            sub_dir_path = sub_dir
        if sub_dir.endswith(".plist"):
            cur_frames_info = {}
            # 读取 plist 文件
            plist_full_path = os.path.join(plist_path, sub_dir)
            print("txt", plist_full_path)
            print("txt_plist_path", plist_path)
            with open(plist_full_path, 'rb') as fp:
                plist_data = plistlib.load(fp)

                frames_dict = plist_data.get('frames', {})

                for png_name, frame_info in frames_dict.items():
                    # 将帧信息存储到字典中，以.png文件名作为键
                    cur_frames_info[png_name] = frame_info
                png_info[index] = cur_frames_info
                index += 1
    i=0
    for png_path in png_file_path:
        num = 0
        print("txt:",png_info[i])
        for png_name, frame_info in png_info[i].items():
            offset_x, offset_y = getDicValue(frame_info['offset'])
            # 1. 提取文件名
            file_name = os.path.basename(png_path)
            # 2. 去掉后缀
            base_name = os.path.splitext(file_name)[0]
            output_sub_dir = os.path.join(file_path, base_name, dir_name)
            if not os.path.exists(output_sub_dir):
                os.makedirs(output_sub_dir)
            out_path = os.path.join(output_sub_dir, f"{num:2d}.txt")
            num += 1
            # 打开文件并写入内容
            with open(out_path, 'w', encoding='utf-8') as file:
                if offset_x>= 0:
                    file.write(str(abs(offset_x)) + '\n')
                    file.write(str(abs(offset_y)))
                else:
                    file.write(str(-abs(offset_x)) + '\n')
                    file.write(str(-abs(offset_y)))
        i += 1
def getCur_Value(file_path,png_path,new_path):
    png_info = {}
    index = 0
    cur_frames_info = {}
    # 读取 plist 文件
    with open(new_path, 'rb') as fp:
        plist_data = plistlib.load(fp)

        frames_dict = plist_data.get('frames', {})

        for png_name, frame_info in frames_dict.items():
            # 将帧信息存储到字典中，以.png文件名作为键
            cur_frames_info[png_name] = frame_info
        png_info[index] = cur_frames_info
        index += 1
    i=0
    num = 0
    print("txt:", png_info[i])
    for png_name, frame_info in png_info[0].items():
        offset_x, offset_y = getDicValue(frame_info['offset'])
        # 1. 提取文件名
        file_name = os.path.basename(png_path)
        # 2. 去掉后缀
        base_name = os.path.splitext(file_name)[0]
        output_sub_dir = os.path.join(file_path, base_name, dir_name)
        if not os.path.exists(output_sub_dir):
            os.makedirs(output_sub_dir)
        out_path = os.path.join(output_sub_dir, f"{num:2d}.txt")
        num += 1
        # 打开文件并写入内容
        with open(out_path, 'w', encoding='utf-8') as file:
            if offset_x >= 0:
                file.write(str(abs(offset_x)) + '\n')
                file.write(str(abs(offset_y)))
            else:
                file.write(str(-abs(offset_x)) + '\n')
                file.write(str(-abs(offset_y)))
    i += 1

def getDicValue(offset_str):
    offset_parts = offset_str[1:-1].split(',')
    offset_x = int(offset_parts[0])  # -103
    offset_y = int(offset_parts[1])  # 66
    return (offset_x, offset_y)